using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace my_newconsole.Report;

public class NZZ_Structural : Base_Structural
{
    public static List<IndexItem> Indexs { get; set; }

    static NZZ_Structural()
    {
        var json = File.ReadAllText("./ReportJson/NZZ.json");
        Indexs = JsonConvert.DeserializeObject<List<IndexItem>>(json);
        Indexs.ForEach(a => a.Code = "NZZ-" + a.Sort);
    }

    public NZZ_Structural(HISRISReportDTO report)
        : base(report) { }

    public override List<IndexItem> GetIndexs()
    {
        return Indexs;
    }

    public override ParmDetails GetDetails(JObject json)
    {
        var details = base.GetDetails(json);
        var rows = json.SelectToken("table.tbody.tr").ToArray();
        // 获取自定义的标题
        var columns = rows[7].SelectToken("td").ToArray();
        var title = columns[0].SelectToken("div")["#text"].Value<string>();
        details.ParmDetail.ForEach(a =>
        {
            a.ParmItemNo = a.ParmItemNo.Replace("doctor custom", title);
            a.ParmEngName = a.ParmEngName.Replace("doctor custom", title);
            a.ParmChnName = a.ParmChnName.Replace("doctor custom", title);
        });
        // 获取最后的备注
        var remarkNode = rows[9].SelectToken("td.div.div");
        if (remarkNode != null)
        {
            var remark = remarkNode["#text"].Value<string>();
            if (!string.IsNullOrWhiteSpace(remark))
            {
                details.ParmDetail.Add(
                    new ParmDetail
                    {
                        ParmItemNo = "remark",
                        ParmEngName = "remark",
                        ParmChnName = "备注",
                        ParmValue = remark,
                        ParmSortNo = (Indexs.Count + 1).ToString(),
                        Remark = ""
                    }
                );
            }
        }
        return details;
    }
}
